<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController {
    public function lists(){
		if($_REQUEST["username"]){
			$name = $_POST["username"];
			$User = M('User'); // 实例化User对象
			$numPerPage = empty($_REQUEST["numPerPage"])?2:$_REQUEST["numPerPage"];
			$count  = $User->where("name = '$name'")->count();// 查询满足要求的总记录数
			$Page  = new \Think\Page($count,$numPerPage);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show  = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $User->where("name = '$name'")->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
			//当前数据
			$this->assign("lists",$list);
			//总条数
			$this->assign("totalCount",$count);
			//每页显示多少条
			$this->assign("numPerPage",$numPerPage);
			//页标数字多少个
			$this->assign("pageNumShown",$Page->totalPages);
			//当前显示第几页
			$page = empty($_REQUEST["pageNum"])?1:$_REQUEST["pageNum"];
			$this->assign("currentPage",$page);
			$this->assign("username",$name);
		}else{
			$User = M('User'); // 实例化User对象
			$numPerPage = empty($_REQUEST["numPerPage"])?2:$_REQUEST["numPerPage"];
			$count  = $User->count();// 查询满足要求的总记录数
			$Page  = new \Think\Page($count,$numPerPage);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show  = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $User->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
			//当前数据
			$this->assign("lists",$list);
			//总条数
			$this->assign("totalCount",$count);
			//每页显示多少条
			$this->assign("numPerPage",$numPerPage);
			//页标数字多少个
			$this->assign("pageNumShown",$Page->totalPages);
			//当前显示第几页
			$page = empty($_REQUEST["pageNum"])?1:$_REQUEST["pageNum"];
			$this->assign("currentPage",$page);
		}
        $this->display();
    }
	//添加表单的方法
	public function addAction(){
		$info = M("user")->add($_POST);
		if($info){
			//成功
			$this->success("添加成功");
		}else{
			//失败
			$this->error("添加失败");
		}
	}
	//删除
	public function del(){
		$id = $_GET["uid"];
		$info = M("user")->where("id = $id")->delete();
		if($info){
			$this->success("删除成功");
		}else{
			$this->error("删除失败");
		}
		
	}
	//修改
	public function edit(){
		$id = $_GET["uid"];
		$info = M("user")->where("id = $id")->find();
		$this->assign("info",$info);
		$this->display();
	}
	public function editAction(){
		$id = $_POST["id"];
		$info = M("user")->where("id = $id")->save($_POST);
		if($info){
			$this->success("修改成功");
		}else{
			$this->error("修改失败");
		}
	}
}